package xyz.luckyli.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.luckyli.springcloud.pojo.Dept;
import xyz.luckyli.springcloud.service.DeptService;

import java.util.List;

/**
 * ClassName:DeptController
 * Package:xyz.luckyli.springcloud.controller
 * Description:
 *
 * @Date:2020/9/29 14:45
 * @Author:857251389@qq.com
 */

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 发现注册服务的一些信息
     */
    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }


    @GetMapping("/dept/get/{id}")
    public Dept queryById(@PathVariable("id") Long id) {
        return deptService.queryById(id);
    }


    @GetMapping("/dept/list")
    public List<Dept> queryAll() {
        return deptService.queryAll();
    }

    @GetMapping("/dept/discovery")
    public Object discovery() {
        List<ServiceInstance> instances = discoveryClient.getInstances("");

        for (ServiceInstance instance : instances) {
            System.out.println(instance.getUri() + "\t" +
                    instance.getHost() + "\t" +
                    instance.getPort() + "\t");
        }

        return this.discoveryClient;
    }
}
